<?php

namespace App\Http\Controllers\Home;

use Illuminate\Support\Facades\Validator;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Models\Home\LoginLog;

class LoginController extends Controller
{
    /**
     * 处理身份认证尝试.
     *
     * @return Response
     */
    public function login(Request $request)
    {
        if($request->isMethod('post')) {

            $validator = Validator::make($request->all(), [

                'username' => 'required',

                'password' => 'required|min:6|max:12',

                'captcha' => 'required|captcha',

                'remember' => 'integer'

            ]);

            if ($validator->fails()) {

                return back()->withErrors($validator)->withInput();

            }else if (Auth::attempt(['username' => $request->post('username'), 'password' => $request->post('password'), 'status' => 1]/*,boolval($request->post('remember'))*/)) {

                LoginLog::create(['uid'=>Auth::id(),'ip'=>$request->getClientIp()]);

                // 认证通过...
                if(Auth::user()->type != 1){

                    return redirect(route('resume_self'));

                }else{

                    return redirect(route('user_index'));

                }

            }

            return back()->withErrors(['用户名或密码错误或账户已被禁用！'])->withInput();
            //return redirect()->back()->with('status', 'Profile updated!');
        }

        return view('Home.Login.login');

    }

    public function logout(){
        Auth::guard('web')->logout();
        return redirect('/');
    }
}